//
//  rightEditDataView.m
//  dataGathering
//
//  Created by apple on 14-11-6.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "rightEditDataView.h"
#import "rightSubTitleVC.h"
#import "currentWorkSetManager.h"
#import "AFViewShaker.h"

@interface rightEditDataView ()
@property (weak, nonatomic) IBOutlet UITextField *nameLabel;
@property (weak, nonatomic) IBOutlet UITextField *descLabel;

@end

@implementation rightEditDataView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_backButton setImage:[UIImage imageNamed:@"u163_mouseOver.png"] forState:UIControlStateHighlighted];
    
    [_backButton addTarget:self action:@selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    currentWorkSetManager *manager = [currentWorkSetManager sharedInstance];
    workSet *currentWorkSet = [manager getCurrentWorkSet];
    [_nameLabel setText:[currentWorkSet name]];
    [_descLabel setText:[currentWorkSet worksetDescription]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)backButtonClicked
{
    [((rightSubTitleVC*)(self.parentViewController.parentViewController)) removeEditDataView];
}

- (IBAction)cancelButtonClicked:(id)sender {
    [self backButtonClicked];
}

- (IBAction)confirmButtonClicked:(id)sender {
    BOOL isEmpty = NO;
    if([_nameLabel.text compare:@""] == NSOrderedSame){
        isEmpty = YES;
        AFViewShaker *shaker = [[AFViewShaker alloc] initWithView:_nameLabel];
        [shaker shakeWithDuration:0.5 completion:^{
            
        }];
        
    }
    if([_descLabel.text compare:@""] == NSOrderedSame){
        isEmpty = YES;
        AFViewShaker *shaker = [[AFViewShaker alloc] initWithView:_descLabel];
        [shaker shakeWithDuration:0.5 completion:^{
            
        }];
    }
    if(!isEmpty){
        currentWorkSetManager *workMgr = [currentWorkSetManager sharedInstance];
        if([workMgr updateWorksetNameAndDesc:[_nameLabel text] :[_descLabel text]])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshTaskCollectionTableView" object:nil];
            [self cancelButtonClicked:nil];
            
        } else {
            
        }
    }

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
